package com.asa.jade;


import com.asa.renderer.Shader;
import com.asa.renderer.Texture;
import com.asa.util.Time;
import org.joml.Vector2f;
import org.lwjgl.BufferUtils;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.glBindVertexArray;
import static org.lwjgl.opengl.GL30.glGenVertexArrays;


public class LevelEditorScene extends Scene{

    private int vertexID,fragmentID,shaderProgram;

    private float[] vertexArray = {
            //position       //color                    //UV 坐标
             100.5f,-5.5f,0.0f,    1.0f,0.0f,0.0f,1.0f,     1,0, //下右 0
            -5.5f, 100.5f,0.0f,    0.0f,1.0f,0.0f,1.0f,     0,1, //上左 1
             100.5f, 100.5f,0.0f,  0.0f,0.0f,1.0f,1.0f,     1,1, //上右 2
            -5.5f,-5.5f,0.0f,      1.0f,1.0f,1.0f,1.0f,     0,0  //下左 3

    };

    private int[] elementArray = {

            2,1,0,//上右三角形
            0,1,3//下左三角形

    };

    private int vaoID,vboID,eboID;

    private Shader defaultShader;

    private Texture testTexture;

    public LevelEditorScene(){


    }

    @Override
    public void init() {
        this.camera = new Camera(new Vector2f());

        defaultShader = new Shader("assets/shaders/default.glsl");

        defaultShader.compile();

        this.testTexture = new Texture("assets/images/testImage.png");


        //生成 VAO,VBO,EBO缓存对象，并发送给GPU

        vaoID = glGenVertexArrays();

        glBindVertexArray(vaoID);

        //顶点浮点缓冲
        FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(vertexArray.length);

        vertexBuffer.put(vertexArray).flip();


        //创建 VBO 上传点缓存
        vboID = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER,vboID);
        glBufferData(GL_ARRAY_BUFFER,vertexBuffer,GL_STATIC_DRAW);

        //创建索引并上传
        IntBuffer elementBuffer = BufferUtils.createIntBuffer(elementArray.length);
        elementBuffer.put(elementArray).flip();

        eboID = glGenBuffers();
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,eboID);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER,elementBuffer,GL_STATIC_DRAW);


        //添加顶点坐标
        int positionsSize = 3;
        int colorSize = 4;
        int uvSize = 2;
        int vertexSizBytes = (positionsSize + colorSize + uvSize) * Float.BYTES;

        glVertexAttribPointer(0,positionsSize,GL_FLOAT,false,vertexSizBytes,0);
        glEnableVertexAttribArray(0);

        glVertexAttribPointer(1,colorSize,GL_FLOAT,false,vertexSizBytes,positionsSize*Float.BYTES);
        glEnableVertexAttribArray(1);

        glVertexAttribPointer(2,uvSize,GL_FLOAT,false,vertexSizBytes,(positionsSize+colorSize)*Float.BYTES);
        glEnableVertexAttribArray(2);


    }

    @Override
    public void update(float dt) {
        camera.position.x -= dt * 50.0f;
        camera.position.y -= dt * 50.0f;

        //绑定程序
        defaultShader.use();

        //上传纹理到shader
        defaultShader.uploadTexture("TEX_SAMPLER",0);
        glActiveTexture(GL_TEXTURE0);
        testTexture.bind();

        defaultShader.uploadMat4f("uProjection",camera.getProjectionMatrix());
        defaultShader.uploadMat4f("uView",camera.getViewMatrix());

        defaultShader.uploadFloat("uTime", Time.getTim());



        //绑定VAO
        glBindVertexArray(vaoID);

        //启用顶点属性指针
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);
        glEnableVertexAttribArray(2);

        glDrawElements(GL_TRIANGLES,elementArray.length,GL_UNSIGNED_INT,0);

        //解绑所有对象

        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);
        glDisableVertexAttribArray(2);

        glBindVertexArray(0);

        defaultShader.detach();

    }


}
